﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using NUnit.Framework;
using Alogient.Cameleon.Sdk;
using Alogient.Cameleon.Sdk.Items;

namespace Alogient.Cameleon.Sdk.tests
{
    [TestFixture]
    public class CMElementTest : FixtureBase
    {
        [SetUp]
        public void Init()
        {
        }

        [TearDown]
        public void Finish()
        {
        }

        [Test]
        public void CreateElement1()
        {
            int navID = this.navID;
            string cultureCode = frCultureCode;
            string name = basicPartName;

            var element = new CMElement(navID, cultureCode, name);

            Assert.AreEqual(navID, element.NavID);
            Assert.AreEqual(cultureCode, element.CultureCode);
            Assert.AreEqual(name, element.ElementName);
        }
        
        protected CMElement LoadElement(int navID, string cultureCode, string name)
        {
            return new CMElement(navID, cultureCode, name);
        }

        [Test]
        public void TestToString()
        {
            var element = this.LoadElement(this.navID, frCultureCode, basicPartName);

            Assert.AreEqual(element.Value, element.ToString());
        }

        [Test]
        public void GetMediaFile()
        {
            var element = this.LoadElement(this.navID, frCultureCode, mediaPartName);

            var path = string.Format("{0}/Media/{1}_{2}_{3}_0_{4}", CMSettings.AdminUrl, this.navID, element.ElementID, element.CultureCode, element.File);

            // Format is Admin Folder Path / Media / NavID _ ElementID _ CultureCode _ 0 _ FileName
            Assert.AreEqual(path, element.GetMediaFile());
        }

        [Test]
        public void GetImageAltText()
        {
            var element = this.LoadElement(this.navID, frCultureCode, imagePartName);

            Assert.AreEqual(element.Value, element.GetImageAltText());
        }
        
        [Test]
        public void ImageAltText()
        {
            var element = this.LoadElement(this.navID, frCultureCode, imagePartName);

            Assert.AreEqual(element.Value, element.ImageAltText);
        }

        [Test]
        [ExpectedException(typeof(CMException))]
        public void GetImageFile1()
        {
            var element = this.LoadElement(this.navID, frCultureCode, basicPartName);

            // Should throw a CMException because the element is not an image
            element.GetImageFile();
        }

        [Test]
        public void GetImageFile2()
        {
            var element = this.LoadElement(this.navID, frCultureCode, imagePartName);

            var path = string.Format("{0}/Media/{1}_{2}_{3}_0_{4}", CMSettings.AdminUrl, this.navID, element.ElementID, element.CultureCode, element.File);

            // Original format
            // Format is Admin Folder Path / Media / NavID _ ElementID _ CultureCode _ 0 _ FileName
            Assert.AreEqual(path, element.GetImageFile());
        }

        [Test]
        public void GetImageFile3()
        {
            var element = this.LoadElement(this.navID, frCultureCode, imagePartName);

            var pathOriginal = string.Format("{0}/Media/{1}_{2}_{3}_0_{4}", CMSettings.AdminUrl, this.navID, element.ElementID, element.CultureCode, element.File);
            var pathPresentation = string.Format("{0}/Media/{1}_{2}_{3}_1_{4}", CMSettings.AdminUrl, this.navID, element.ElementID, element.CultureCode, element.File);
            var pathThumbnail = string.Format("{0}/Media/{1}_{2}_{3}_2_{4}", CMSettings.AdminUrl, this.navID, element.ElementID, element.CultureCode, element.File);

            // Original format
            // Format is Admin Folder Path / Media / NavID _ ElementID _ CultureCode _ 0 _ FileName
            Assert.AreEqual(pathOriginal, element.GetImageFile(ImageFileType.Original));

            // Presentation format
            // Format is Admin Folder Path / Media / NavID _ ElementID _ CultureCode _ 1 _ FileName
            Assert.AreEqual(pathPresentation, element.GetImageFile(ImageFileType.Presentation));

            // Thumbnail format
            // Format is Admin Folder Path / Media / NavID _ ElementID _ CultureCode _ 2 _ FileName
            Assert.AreEqual(pathThumbnail, element.GetImageFile(ImageFileType.Thumbnail));
        }

        [Test]
        public void GetImageWidth1()
        {
            var element = this.LoadElement(this.navID, frCultureCode, imagePartName);

            Assert.AreEqual(0, element.GetImageWidth());
        }

        [Test]
        public void GetImageWidth2()
        {
            var element = this.LoadElement(this.navID, frCultureCode, imagePartName);

            Assert.AreEqual(0, element.GetImageWidth(ImageFileType.Original));

            Assert.AreEqual(0, element.GetImageWidth(ImageFileType.Presentation));

            Assert.AreEqual(0, element.GetImageWidth(ImageFileType.Thumbnail));
        }

        [Test]
        public void GetImageHeight1()
        {
            var element = this.LoadElement(this.navID, frCultureCode, imagePartName);

            Assert.AreEqual(0, element.GetImageHeight());
        }

        [Test]
        public void GetImageHeight2()
        {
            var element = this.LoadElement(this.navID, frCultureCode, imagePartName);

            Assert.AreEqual(0, element.GetImageHeight(ImageFileType.Original));

            Assert.AreEqual(0, element.GetImageHeight(ImageFileType.Presentation));

            Assert.AreEqual(0, element.GetImageHeight(ImageFileType.Thumbnail));
        }
    }
}
